﻿namespace NavigateTo2013
{
    using EnvDTE;
    using Microsoft.VisualStudio;
    using Microsoft.VisualStudio.Language.NavigateTo.Interfaces;
    using Microsoft.VisualStudio.Shell;
    using System.Collections.ObjectModel;
    using System.Drawing;
    using System.IO;
    using System.Windows.Input;

    public class NavigateToItemDisplay : INavigateToItemDisplay2
    {
        public NavigateToItemDisplayFactory Factory { get; set; }

        public string FileName { get; set; }
        
        public string FilePath { get; set; }

        public Task Task { get; set; }

        public string AdditionalInformation
        {
            get 
            {
                return this.FilePath;
            }
        }

        public string Description
        {
            get 
            { 
                return this.FilePath; 
            }
        }

        public ReadOnlyCollection<DescriptionItem> DescriptionItems
        {
            get 
            {
                return null;
            }
        }

        public Icon Glyph
        {
            get 
            {
                return IconCache.GetIcon(this.FilePath);
            }
        }

        public string Name
        {
            get 
            {
                if (this.FileName == SearchEngine.More)
                {
                    return "more...";
                }

                return this.FileName;
            }
        }

        public void NavigateTo()
        {
            if (this.FileName == SearchEngine.More)
            {
                return;
            }

            try
            {
                if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
                {
                    System.Diagnostics.Process.Start(this.FilePath);
                    return;
                }
                else if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
                {
                    System.Diagnostics.Process.Start(Path.GetDirectoryName(this.FilePath));
                    return;
                }

                DTE dte = (DTE)ServiceProvider.Current.GetService(typeof(DTE));
                var window = dte.ItemOperations.OpenFile(this.FilePath);
                if (window != null)
                {
                    window.Activate();
                }

                Find finder = dte.Find;
                if (finder != null)
                {
                    finder.Action = vsFindAction.vsFindActionFind;
                    finder.Target = vsFindTarget.vsFindTargetCurrentDocument;
                    finder.FindWhat = this.Task.SearchValue.Replace("''", "'");
                    finder.Execute();
                }
            }
            catch
            {
            }
        }

        public int GetProvisionalViewingStatus()
        {
            if (this.FileName == SearchEngine.More)
            {
                return 3;
            }

            if (string.IsNullOrEmpty(this.FilePath))
            {
                return 0;
            }

            try
            {
                return (int)VsShellUtilities.GetProvisionalViewingStatus(this.FilePath);
            }
            catch
            {
            }

            return 0;
        }

        public void PreviewItem()
        {
            if (this.FileName == SearchEngine.More && !this.Factory.IsMoreClicked)
            {
                this.Task.MaxCount += 20;                
                this.Factory.IsMoreClicked = true;
                this.Task.Provider.RequeueTask(this.Task);
                return;
            }

            ErrorHandler.CallWithCOMConvention(delegate
            {
                this.NavigateTo();
            }, false);
        }
    }
}